<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="专题id" prop="specialId">
      <el-input v-model="dataForm.specialId" placeholder="专题id"></el-input>
    </el-form-item>
    <el-form-item label="专题作业/题库表id" prop="ssWorkId">
      <el-input v-model="dataForm.ssWorkId" placeholder="专题作业/题库表id"></el-input>
    </el-form-item>
    <el-form-item label="问题名称" prop="question">
      <el-input v-model="dataForm.question" placeholder="问题名称"></el-input>
    </el-form-item>
    <el-form-item label="问题答案" prop="answer">
      <el-input v-model="dataForm.answer" placeholder="问题答案"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="day">
      <el-input v-model="dataForm.day" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="创建用户id" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建用户id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          specialId: '',
          ssWorkId: '',
          question: '',
          answer: '',
          day: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          specialId: [
            { required: true, message: '专题id不能为空', trigger: 'blur' }
          ],
          ssWorkId: [
            { required: true, message: '专题作业/题库表id不能为空', trigger: 'blur' }
          ],
          question: [
            { required: true, message: '问题名称不能为空', trigger: 'blur' }
          ],
          answer: [
            { required: true, message: '问题答案不能为空', trigger: 'blur' }
          ],
          day: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建用户id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/avanotes/questionbankdetail/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.specialId = data.questionBankDetail.specialId
                this.dataForm.ssWorkId = data.questionBankDetail.ssWorkId
                this.dataForm.question = data.questionBankDetail.question
                this.dataForm.answer = data.questionBankDetail.answer
                this.dataForm.day = data.questionBankDetail.day
                this.dataForm.createUserId = data.questionBankDetail.createUserId
                this.dataForm.createTime = data.questionBankDetail.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/avanotes/questionbankdetail/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'specialId': this.dataForm.specialId,
                'ssWorkId': this.dataForm.ssWorkId,
                'question': this.dataForm.question,
                'answer': this.dataForm.answer,
                'day': this.dataForm.day,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
